package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.Topic;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/6/26.
 */
public class TopicLinearLayoutAdapter extends RecyclerView.Adapter<TopicLinearLayoutAdapter.TopicLinearViewholder>{

    private LayoutInflater inflater;

    private JSONArray topicList;

    private Context context;

    private mItemOnClickListener itemOnClickListener;


    public TopicLinearLayoutAdapter(Context context , JSONArray topicList) {
        this.topicList = topicList;
        this.context = context;

        inflater = LayoutInflater.from(context);
    }

    /**
     * 设置监听
     * @param itemOnClickListener
     */
    public void setItemOnClickListener(mItemOnClickListener itemOnClickListener)
    {
        this.itemOnClickListener = itemOnClickListener;
    }


    @Override
    public TopicLinearViewholder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = inflater.inflate(R.layout.topic_item,null);

        return new TopicLinearViewholder(itemView);
    }

    @Override
    public void onBindViewHolder(TopicLinearViewholder holder, final int position) {

        try {
            JSONObject obj = topicList.getJSONObject(position);
            JSONObject object = obj.getJSONObject("topic");

            Topic topic = Topic.objectFromData(object.toString());

            LogUtil.w(">>>>>>>>>>>>>>>>>>>>");
            LogUtil.w("mainurl = " + topic.getImage_url() );

            ImageLoader.getInstance().displayImage(topic.getImage_url(),holder.ivmian, ImageUtil.getDefaultOptions());

            holder.tvtitle.setText(topic.getTitle());
            holder.tvcontent.setText(topic.getDescribe());
            holder.tvcomment.setText(topic.getComment_num()+"");
            holder.tvview.setText(topic.getConcern_num()+"");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.OnItemClick(position);
            }
        });



    }

    @Override
    public int getItemCount() {

        return topicList == null ? 0 : topicList.length();
    }

    public class TopicLinearViewholder extends RecyclerView.ViewHolder
    {
        ImageView ivmian;
        TextView tvtitle,tvcontent,tvcomment,tvview;

        public TopicLinearViewholder(View itemView) {
            super(itemView);

            ivmian = (ImageView) itemView .findViewById(R.id.topic_item_main_iv);
            tvtitle = (TextView) itemView.findViewById(R.id.topic_item_title_tv);
            tvcontent = (TextView) itemView.findViewById(R.id.topic_item_content_tv);
            tvcomment = (TextView) itemView.findViewById(R.id.topic_item_comment_tv);
            tvview = (TextView) itemView.findViewById(R.id.topic_item_view_tv);

        }
    }

    public interface mItemOnClickListener
    {
        void OnItemClick(int position);
    }
}
